package src.main.java.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import src.main.java.entity.ClienteTO;
import src.main.java.modules.container.AutenticacaoCaller;
import src.main.java.modules.container.ClientesCaller;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware {
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map<String, Object> session;
	  
	private ClienteTO cliente;
	private String origem;
	private String status;
	private String action;
	
	private String feedback;
	
	@Action(value = "/login", results = {
			@Result(location = "login.jsp", name = "sucess"),
		}
	)
	public String telaLogin(){
		return "sucess";
	}
	
	@Action(value = "/logarAction", results = {
			@Result(location = "actionRedirect.jsp", name = "redirect"), 
			@Result(location = "login.jsp", name = "fail") }
	)
	public String logar(){
		
		HttpSession session = request.getSession(true);  
		
		/*Autentica com compenente AUTENTICACAO*/
		if (AutenticacaoCaller.Login(cliente.getCpf(), cliente.getSenha())){
			/*Faz uma chamada ao componente CLIENTE para pegar seus dados*/
			setFeedback("loginSucesso");
			cliente = ClientesCaller.getCliente(cliente.getCpf());
		} else {
			setFeedback("naoAutenticado");
			return "fail";
		}
		
		session.setAttribute("cliente", cliente);
		setStatus("login");

		if ("carrinho".equals(origem)){
			System.out.println("Login a partir do Carrinho - Redirecionando");
			setAction("finalizarCompra");
		} else {
			setAction(""); //home
		}

		return "redirect";
		
	}
	
	@Action(value = "/deslogarCliente", results = {
			@Result(location = "actionRedirect.jsp", name = "sucess")} 
	)
	public String sair(){
		setAction("");
		session.remove("cliente");
		return "sucess";
	}

	public String getOrigem() {
		return origem;
	}

	public void setOrigem(String origem) {
		this.origem = origem;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public ClienteTO getCliente() {
		return cliente;
	}

	public void setCliente(ClienteTO cliente) {
		this.cliente = cliente;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getFeedback() {
		return feedback;
	}

	public void setFeedback(String feedback) {
		this.feedback = feedback;
	}

}
